package codetop.kuaishou.T199;

import util.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 18362
 * @Create: 2022-09-14 13:00:29 星期三
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null)
            return res;
        Deque<TreeNode> dq = new LinkedList<>();
        dq.offer(root);
        while (!dq.isEmpty()) {
            int size = dq.size();
            for (int i = 0; i < size; i++) {
                TreeNode front = dq.poll();
                if (i == size - 1)
                    res.add(front.val);
                if (front.left != null)
                    dq.offer(front.left);
                if (front.right != null)
                    dq.offer(front.right);
            }
        }
        return res;
    }
}